#!/usr/bin/env python

# (c) 2007-2011 Helsinki University of Technology and University of Helsinki
# Licensed under the MIT license; see LICENSE.txt for more information.

from worker import Worker

from importer.dchtml import DCHTMLImporter
from importer.rdf import RDFImporter
try:
  from importer.rdfa import RDFaImporter
except ImportError:
  pass	# pyRdfa not installed?
from importer.uf import MicroformatImporter

import sys

class Importer (Worker):
  def __init__(self, session):
    Worker.__init__(self, session.getFatalEvent())
    self._session = session
    
    imptype = session.getSource().getType()
    if imptype == 'html':
      self._importer = DCHTMLImporter(session)
    elif imptype == 'rdf':
      self._importer = RDFImporter(session)
    elif imptype == 'rdftweaks':
      self._importer = RDFTweaksImporter(session)
    elif imptype == 'rdfa':
      try:
        self._importer = RDFaImporter(session)
      except NameError:
        print >>sys.stderr, "ERROR: RDFa data sources require pyRdfa."
        self._importer = None
        return
    elif imptype == 'microformat':
      self._importer = MicroformatImporter(session)
    else:
      self._importer = None
      print >>sys.stderr, "ERROR: Unknown importer type", session.getSource().getType()
      return
            

  def process(self, doc):
    if not self._importer:
      return False

    return self._importer.process(doc)

  def finish(self):
    if self._importer:
      self._importer.finish()
